<?php
	
/* 
	
	dkpm - a DKP management system
	
	The contents of this file are subject to the Mozilla Public License
	Version 1.1 (the "License"); you may not use this file except in
	compliance with the License. You may obtain a copy of the License at
	http://www.mozilla.org/MPL/

	Software distributed under the License is distributed on an "AS IS"
	basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See the
	License for the specific language governing rights and limitations
	under the License.
	
	The Original Code is Copyright (C) 2007 Martin Wedvich. All Rights Reserved.
	
	-
	
	uploadlog.php
	This page deals with uploading combat logs.

*/
	
	define('dkpm', true);
	session_start();
	
	require 'functions.php';
	require 'framework.php';
	
	$uploadstatus = 'generic';
	
	if(!isset($_FILES['combatlog'])) {
		$addcontent = '
						<p class="botspace"><span class="header1">Upload combat log</span></p>';
	} else {
		if($_FILES['combatlog']['error'] == 0) {
	 		$tempname = 'temp/' . randomize_string(16);
			move_uploaded_file($_FILES['combatlog']['tmp_name'], $tempname);
			$combatlog = file($tempname);
			for($i = 0; $i <= count($combatlog) - 1; $i++) {
				if(strstr($combatlog[$i], 'fail to cast') || strstr($combatlog[$i], 'fails to cast')) {
					// trim the "you fail to cast" stuff which shows up if you spam your abilities regardless of GCD
					$combatlog[$i] = 'pop';
				} else {
				 	// get rid of line breaks
					$combatlog[$i] = trim($combatlog[$i]);
				}
			}
			// this and the following loop is there to actually delete the "you fail to cast" stuff before storing the array
			sort($combatlog);
			for($i = count($combatlog) - 1; $i >= 0; $i--) {
				if($combatlog[$i] === 'pop') {
					array_pop($combatlog);
				}
			}
			$combatlog = implode(';', $combatlog);
			unlink($tempname);
			$character = mysql_result(mysql_query('SELECT character_id FROM dkpm_characters WHERE character_name="' . $_POST['recordedby'] . '"'), 0, 0);
			mysql_query('INSERT INTO dkpm_combatlogs (log_character, log_data) VALUES (' . $character . ', "' . $combatlog . '")');
			$uploadstatus = 'ok';
			$addtitle = ' successful';
			$addcontent = '
						<p><span class="header1ok">Upload successful</span></p>
						<p class="botspace"><span class="sub1ok">The combat log was uploaded successfully.</span></p>';
			
		} else {
			$uploadstatus = 'failed';
			$addtitle = ' failed';
			$addcontent = '
						<p><span class="header1failed">Upload failed</span></p>
						<p class="botspace"><span class="sub1failed">The combat log could not be uploaded.</span></p>';
		}
	}
	
	$title = 'dkpm &raquo; Upload combat log' . $addtitle;
	if($uploadstatus != 'ok') {
	 	$sql = mysql_query('SELECT character_name FROM dkpm_characters WHERE character_user=' . $_SESSION['user']['id']);
	 	$characters = '';
	 	while($character = mysql_fetch_row($sql)) {
			$characters .= '
							<option value="' . $character[0] . '">' . $character[0] . '</option>';
		}
		$content = '<div id="formbox" class="' . $uploadstatus . '">
					<form name="uploadform" enctype="multipart/form-data" method="post" action="uploadlog.php">' . $addcontent . '
						<p>File</p>
						<input type="hidden" name="MAX_FILE_SIZE" value="1048576" />
						<p><input name="combatlog" type="file" /></p>
						<p>Recorded by</p>
						<select name="recordedby">' . $characters . '
						</select>
						<p class="topspace">
							<input type="submit" value="Upload" /><input type="reset" value="Reset" />
						</p>
					</form>
				</div>';	
	} else {
		$content = '<div id="formbox" class="' . $uploadstatus . '">
					<form name="uploadform" enctype="multipart/form-data" method="post" action="login.php">' . $addcontent . '
						<a href="index.php" title="Return to index">Return to index</a>.
					</form>
				</div>';	
	}	
		
	framework_exec($title, $content);

?>